package com.liang.drug.config;/*
 *@Author: ljy
 *@Date:2021/11/1 22:39
 *
 */

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.swing.*;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 权限控制shiro配置类
 */
@Configuration
public class ShiroFilterConfiguration {
    /**
     * 创建过滤工厂Bean
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean() {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager());
        /**
         * shiro内置过滤器 ，实现权限相关的拦截器
         * anon:无需登录
         * authc:必须登录才可以访问
         */
        Map<String,String> filterMap=new LinkedHashMap<>();
        filterMap.put("/static/**","anon");// static页面下的静态资源
        filterMap.put("/login","anon");//login 页面
        filterMap.put("/toLogin","anon");//用户登录请求
        filterMap.put("/**","authc");//其他用户登录了才可以操作
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        shiroFilterFactoryBean.setLoginUrl("/login");//设置用户默认登录页面（未登录）
        shiroFilterFactoryBean.setSuccessUrl("/index");//登录成功跳转的页面
        return shiroFilterFactoryBean;
    }

    /**
     * 权限管理
     */
    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm());
        return securityManager;
    }

    /**
     * 创建Realm,认证用
     *
     * @return
     */
    @Bean
    public UserRealm userRealm() {
        UserRealm userRealm = new UserRealm();
        return userRealm;
    }

    /**
     * thymeleaf整合shiro
     * @return
     */
    @Bean
    public ShiroDialect shiroDialect(){
    return  new ShiroDialect();
    }
}
